package com.itheima.controller;

import com.itheima.Service.CheckgroupService;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/checkgroup")
public class CheckgroupController {

    @Autowired
    private CheckgroupService checkgroupService;

    /**
     * 新增检查组
     * @param checkGroup
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup,String checkitemIds){
        log.info("新增检查组成功");
        checkgroupService.add(checkGroup,checkitemIds);
        return new Result(true,"新增检查组成功",null);
    }

    /**
     * 根据id删除检查组
     * @param id
     * @return
     */
    @GetMapping("/deleteById")
    public Result deleteById( String id){
        log.info("删除了id为{}的一个检查组",id);
        checkgroupService.deleteById(id);
        return new Result(true,"删除检查组成功",null);
    }

    /**
     * 根据套餐ID查询检查组ID
     * @param setmealId
     * @return
     */
    @GetMapping("/findCheckgroupIdsBySetmealId")
    public Result findCheckgroupIdsBySetmealId(String setmealId){
        List<Integer> lists = checkgroupService.findCheckGroupIdsBySetmealId(setmealId);
        return new Result(true,"查询检查组成功",lists);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        log.info("页面查询成功：查询到{}条数据",queryPageBean.getPageSize());
        PageResult pageResult = checkgroupService.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * 编辑检查组
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup ,String checkitemIds){
        checkgroupService.edit(checkGroup,checkitemIds);
        return new Result(true,"编辑检查组成功",null);
    }

    /**
     * 查询所有检查组
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        List<CheckGroup> lists = checkgroupService.findAll();
        log.info("查询所有检查组成功，共查询到{}条数据",lists.size());
        return new Result(true,"查询检查组成功",lists);
    }

    /**
     * 根据id查找检查组
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(String id){
        CheckGroup checkGroup = checkgroupService.findById(id);
        return new Result(true,"查询检查组成功",checkGroup);
    }
}
